<?php
class OrderAction extends CommonAction {
	
	/**
	 * 删除报名单(实际改变订单状态逻辑删除)
	 */
	public function delOrder() {
		$result = false;
		$Order = D ( "Order" );
		$ids = $_POST['ids'];	
		$data["DD_DDZT"]  = 'DDZT4';
		
		$arrayIds = explode(',',$ids);
			
		$Order->startTrans();		
		foreach ($arrayIds as $id){
			//$result = $Order->relation(true)->delete($id);			
			$result = $Order->where('Id='.$id)->save($data);
			
			$ddbh = $Order->where('Id='.$id)->field('DD_BH')->select(); //获取订单编号
			
			$OrderListObject = D("OrderList");
			$courseDetail = $OrderListObject->where('LS_DDBH ="'.$ddbh[0]['DD_BH'].'"')->field('LS_KCBH')->select();//获取该订单下所有课程编号
			
			foreach ($courseDetail as $orderCourse) { //改变该订单下所有课程已报名人数
				$Course = M("Course");
				$ybrs = $Course->where('Id='.$orderCourse['LS_KCBH'])->field('KC_YBRS')->select();
				
				$kc_ybrs = $ybrs[0]['KC_YBRS'] - 1;
				if($kc_ybrs < 0){
					$kc_ybrs = 0;
				}
				$Course-> where('Id='.$orderCourse['LS_KCBH'])->setField('KC_YBRS',$kc_ybrs);
			}
				
			$OrderState = A('OrderState'); //实例订单跟踪action
			$result = $OrderState->saveDDGZ($ddbh[0]['DD_BH'],'DDZT4',$_SESSION['loginUserId'],$_SESSION['loginUserName']); //添加订单跟踪记录
		}		

		if($result){
			$Order->commit();
		}else{
			$Order->rollback();
		}
		if ($result == false) {
			echo json_encode (array(
					'msg' => '删除出错！'
			));
		} else {
			echo json_encode (array(
					'success' => true
			));
		}
	}
	
	/**
	 * 修改报名单
	 */
	public function saveOrder() {
		$result = false;
		$Order = D("Order");	
		$id = $_REQUEST['id'];
		$DD_BH = $_POST ['DD_BH'];
		
		$data = array();
		$data["DD_BH"] = $_POST ['DD_BH'];
		$data["DD_KHBH"]  = $_POST ['DD_KHBH'];
		$data["DD_KHMC"]  = $_POST["DD_KHMC"];	
		$data["DD_KHPY"]  = $_POST["DD_KHPY"];
		$data["DD_FP"]  = $_POST["DD_FP"];
		$data["DD_FPTT"]  = $_POST["DD_FPTT"];
		$data["DD_FPNN"]  = $_POST["DD_FPNN"];
		$data["DD_PPMC"]  = $_POST["DD_PPMC"];
		$data["DD_SL"]  = $_POST["DD_SL"];
		$data["DD_YF"]  = $_POST["DD_YF"];
		$data["DD_SF"]  = $_POST["DD_SF"];
		$data["DD_ZJE"]  = $_POST["DD_ZJE"];
		$data["DD_ZK"]  = $_POST["DD_ZK"];
		$data["DD_SYYHQ"]  = $_POST["DD_SYYHQ"];
		$data["DD_ZFFS"]  = $_POST["DD_ZFFS"];
		$data["DD_ZD"]  = $_POST["DD_ZD"];
		$data["DD_DDZT"]  = $_POST["DD_DDZT"];
		$data["DD_CJSJ"]  = $_POST["DD_CJSJ"];
		$data["DD_TJR"]  = $_POST["DD_TJR"];		
		$sourcesData = $_POST["courseList"];		
 		$OrderList = D("OrderList");
 		
 		$Order->startTrans();
 		/**
		 * 先删除该订单下的课程
 		 */		
 		$ddbh = $Order->where('Id='.$id)->field('DD_BH')->select(); //获取订单编号 			
 		$OrderListObject = D("OrderList");
 		$courseDetail = $OrderListObject->where('LS_DDBH ="'.$ddbh[0]['DD_BH'].'"')->field('LS_KCBH')->select();//获取该订单下所有课程编号			
 		foreach ($courseDetail as $orderCourse) { //改变该订单下所有课程已报名人数
 			$Course = M("Course");
 			$ybrs = $Course->where('Id='.$orderCourse['LS_KCBH'])->field('KC_YBRS')->select(); 		
 			$kc_ybrs = $ybrs[0]['KC_YBRS'] - 1;
 			if($kc_ybrs < 0){
 				$kc_ybrs = 0;
 			}
 			$Course-> where('Id='.$orderCourse['LS_KCBH'])->setField('KC_YBRS',$kc_ybrs);
 		}
 		$result = $OrderList->where('LS_DDBH="'.$DD_BH .'"')->delete();
 		
 		/**
 		 * 重新生成该订单课程关联
 		 */
 		foreach ($sourcesData as $source) {
 			$OrderListObject = D("OrderList");
 			$obj = array();			
 			$obj["LS_DDBH"] = $data["DD_BH"];
 			$obj["LS_KCBH"] = $source["LS_KCBH"];
 			$obj["LS_KCJG"] = $source["LS_KCJG"];
 			$obj["LS_KCMC"] = $source["LS_KCMC"];
 			$obj["LS_KCLX"] = $source["LS_KCLX"];
 			$obj["LS_JSBH"] = $source["LS_JSBH"];
 			$obj["LS_JSMC"] = $source["LS_JSMC"];
 			$obj["LS_ZK"] = $source["LS_ZK"];
 			$obj["LS_SL"] = $source["LS_SL"];
 			$obj["LS_YFJE"] = $source["LS_YFJE"];
 			$obj["LS_SRQ"] = $source["LS_SRQ"];
 			$obj["LS_STIME"] = $source["LS_STIME"];
 			$obj["LS_ETIME"] = $source["LS_ETIME"];
 			$obj["LS_KCDW"] = $source["LS_KCDW"];
 			$obj["LS_KCDWMC"] = $source["LS_KCDWMC"];
 			$OrderListObject->add($obj);
 			
 			//更新课程报名人数
 			$courseID = $source["LS_KCBH"];
 			$condition['Id'] = $courseID;
 			$Course = M("Course");
 			$ybrs = $Course->where($condition)->field('KC_YBRS')->select();
 			$kc_ybrs = $ybrs[0]['KC_YBRS'] + 1;
 			$Course-> where('Id='.$courseID)->setField('KC_YBRS',$kc_ybrs);
 		} 				
		$result = $Order->where('Id='.$id)->save($data);	
		
		$OrderState = A('OrderState'); //实例订单跟踪action
		$result = $OrderState->saveDDGZ($DD_BH,'DDZT10',$_SESSION['loginUserId'],$_SESSION['loginUserName']); //添加订单跟踪记录
		
		if($result){
			$Order->commit();
		}else{
			$Order->rollback();
		}
		
		if ($result == true) {
			echo json_encode ( array (
					'success' => true 
			) );
		} else {
			echo json_encode ( array (
					'errorMsg' => '更新失败！' 
			));
		}
	}
	
	
	/**
	 * 添加报名单
	 */
	public function addOrder() {
		$result = false;
		$Order = D("Order");	
		$dd_cjsj = date("Y-m-d H:i:s", time());//获取系统当前时间
		$dd_xq = "HYXQ";
		if(isset($_SESSION['xqid'])) {
			$dd_xq =$_SESSION['xqid'];
		}
		$dd_cjr = $_SESSION['loginUserName'];
		
		$dd_ddbh = $Order->getOrderBH($dd_xq,$dd_cjsj);
		$data = array();
		$data["DD_BH"] = $dd_ddbh;
		$data["DD_KHBH"]  = $_POST ['DD_KHBH'];
		$data["DD_KHMC"]  = $_POST["DD_KHMC"];	
		$data["DD_KHPY"]  = $_POST["DD_KHPY"];
		$data["DD_FP"]  = $_POST["DD_FP"];
		$data["DD_FPTT"]  = $_POST["DD_FPTT"];
		$data["DD_FPNN"]  = $_POST["DD_FPNN"];
		$data["DD_PPMC"]  = $_POST["DD_PPMC"];
		$data["DD_SL"]  = $_POST["DD_SL"];
		$data["DD_YF"]  = $_POST["DD_YF"];
		$data["DD_SF"]  = $_POST["DD_SF"];
		$data["DD_ZJE"]  = $_POST["DD_ZJE"];
		$data["DD_ZK"]  = $_POST["DD_ZK"];
		$data["DD_SYYHQ"]  = $_POST["DD_SYYHQ"];
		$data["DD_ZFFS"]  = $_POST["DD_ZFFS"];

		$data["DD_ZD"] = $dd_xq;
		$data["DD_DDZT"]  = $_POST["DD_DDZT"];
		$data["DD_CJSJ"]  = $dd_cjsj;//获取系统当前时间
		$data["DD_CJRMC"]  = $dd_cjr;//创建人
		$data["DD_TJR"]  = $_POST["DD_TJR"];	
			
		$Order->startTrans();
		$sourcesData = $_POST["courseList"];
		foreach ($sourcesData as $source) {
				$OrderListObject = D("OrderList");
				$obj = array();
				$obj["LS_DDBH"] = $data["DD_BH"];
				$obj["LS_KCBH"] = $source["LS_KCBH"];
				$obj["LS_KCJG"] = $source["LS_KCJG"];
				$obj["LS_KCMC"] = $source["LS_KCMC"];
				$obj["LS_KCLX"] = $source["LS_KCLX"];
				$obj["LS_JSBH"] = $source["LS_JSBH"];
				$obj["LS_JSMC"] = $source["LS_JSMC"];
				$obj["LS_ZK"] = $source["LS_ZK"];
				$obj["LS_SL"] = $source["LS_SL"];
				$obj["LS_YFJE"] = $source["LS_YFJE"];
				$obj["LS_SRQ"] = $source["LS_SRQ"];
				$obj["LS_STIME"] = $source["LS_STIME"];
				$obj["LS_ETIME"] = $source["LS_ETIME"];		
				$obj["LS_KCDW"] = $source["LS_KCDW"];
				$obj["LS_KCDWMC"] = $source["LS_KCDWMC"];
				$OrderListObject->add($obj); //直接添加
				
				//更新课程报名人数
				$courseID = $source["LS_KCBH"];
				$condition['Id'] = $courseID;
				$Course = M("Course");				
				$ybrs = $Course->where($condition)->field('KC_YBRS')->select();
				$kc_ybrs = $ybrs[0]['KC_YBRS'] + 1;
				$Course-> where('Id='.$courseID)->setField('KC_YBRS',$kc_ybrs);
		}	
		$result = $Order->add($data); //添加普通订单
		$OrderState = A('OrderState'); //实例订单跟踪action
		$result = $OrderState->saveDDGZ($dd_ddbh,$_POST["DD_DDZT"],$_SESSION['loginUserId'],$_SESSION['loginUserName']); //添加订单跟踪记录
		if($result){
			$Order->commit();
		}else{
			$Order->rollback();
		}
// 		if($evalTemp!=0){ //表明有预订报名单，保存为另一个新订单（类型为预订订单）
// 			$data["DD_BH"] = $Order->getOrderBH($_POST ['DD_ZD'],$_POST["DD_CJSJ"]);
// 			$data["DD_PPMC"]  = $_POST["DD_PPMC_UNNOMAL"];
// 			$data["DD_DDZT"] = "5";
// 			$data["DD_YF"]  = "0";
// 			$data["DD_SF"]  = "0";
// 			$data["DD_ZJE"]  = "0";				
// 			$result = $Order->add($data); //添加普通订单
// 			foreach ($sourcesData as $source) {
// 				if($source["LS_TYPE"] == "1") {//是否为预报名单 1-预报名 订单            0-普通订单
// 					$OrderListObject = D("OrderList");
// 					$obj = array();
// 					$obj["LS_DDBH"] = $data["DD_BH"];
// 					$obj["LS_KCBH"] = $source["LS_KCBH"];
// 					$obj["LS_KCJG"] = $source["LS_KCJG"];
// 					$obj["LS_KCMC"] = $source["LS_KCMC"];
// 					$obj["LS_KCLX"] = $source["LS_KCLX"];
// 					$obj["LS_JSBH"] = $source["LS_JSBH"];
// 					$obj["LS_JSMC"] = $source["LS_JSMC"];
// 					$obj["LS_ZK"] = $source["LS_ZK"];
// 					$obj["LS_SL"] = $source["LS_SL"];
// 					$obj["LS_YFJE"] = $source["LS_YFJE"];
// 					$OrderListObject->add($obj); //直接添加
// 				}
// 			}
// 		}			
		//$result = $Order->relation("OrderList")->add($data);
		if($result) {
			echo json_encode (array (
					'success' => true
			));
		}else{
			echo json_encode (array('msg'=>'添加出错！'));
		}
	}		

	/**
	 * 查询(non-PHPdoc)
	 * @see CommonAction::read()
	 */
	public function readOrder() {
		$map = " and dd.DD_DDZT = 'DDZT1'";		//显示为未缴费订单
		
		if(!empty($_POST['DD_BH'])) {
			$map = $map." and dd.DD_BH like '%".$_POST['DD_BH']."%'";
		}
		if(!empty($_POST['DD_KHBH'])) {
			$map = $map." and dd.DD_KHBH like '%".$_POST['DD_KHBH']."%'";
		}
		if(!empty($_POST['DD_KHMC'])) {
			//$map = $map ." and dd.DD_KHMC like '%".$_POST['DD_KHMC']."%'";
			$map = $map ." and (dd.DD_KHPY like '%".$_POST['DD_KHMC']."%' or dd.DD_KHMC like '%".$_POST['DD_KHMC']."%')";
		}
		if(!empty($_POST['DD_PPMC'])) {
			$map = $map ." and dd.DD_PPMC like '%".$_POST['DD_PPMC']."%'";
		}
		
		$xqid =  $_SESSION['XqFilterString'] ;//获取用户当前拥有校区权限
		if(!empty($xqid)){
			$map = $map." and dd.DD_ZD in (".$xqid.") ";
		}
		
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		
		$Order = M ( "Order" );
		
		$arrNum = $Order->query("select count(*) as num from smis_order dd where 1=1 " . $map);
		$row = $arrNum[0]['num'];
		$total = $row;
		
		$Orders = array ();
		$Orders = $Order->table('smis_order dd, smis_dictionary_list dictionary,smis_dictionary_list dictionary1,smis_dictionary_list dictionary2')
						->where('dd.DD_ZFFS = dictionary.ITEM_KEY and dd.DD_DDZT = dictionary1.ITEM_KEY and dd.DD_ZD = dictionary2.ITEM_KEY' .$map)
						->field('dd.*,dictionary.ITEM_VALUE as ZFFS,dictionary1.ITEM_VALUE as DDZT,dictionary2.ITEM_VALUE as ZD')
						->order('dd.DD_CJSJ desc' )
		                ->page($pagenum,$rowsnum)->select();
		
		$resultJson = json_encode($Orders);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
		
	}
	
	/**获取课程列表信息
	 * 查询(non-PHPdoc)
	 * @see CommonAction::read() 
	 * */
	public function getCourseList() {
		
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$course = D ( "Course" );
		$map = " and course.status ='1' " ;
		
		$xqid =  $_SESSION['XqFilterString'] ;//获取用户当前拥有校区权限
		if(!empty($xqid)){
			$map = $map." and course.KC_XQ in (".$xqid.") ";
		}
		if(!empty($_POST['KC_NJ'])) {
			$map = $map." and course.KC_NJ in ('".$_POST['KC_NJ']."','nj0')";
		}
		$arrNum = $course->query("select count(*) as num from smis_course course where 1=1 " . $map);
		$row = $arrNum[0]['num'];
		$total = $row;
		
		$courseList = array ();
		$courseList = $course->table('smis_course course, smis_dictionary_list dictionary')
		->where('course.KC_KMBH = dictionary.ITEM_KEY'.$map)
		->field('course.*,dictionary.ITEM_VALUE as KMMC')
		->order('course.KC_NJ desc')
		->page($pagenum,$rowsnum)->select();
		
		$resultJson = json_encode($courseList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}
	
	
	/**
	 * 获取报名单流水明细
	 */
	public function  readOrderDetails(){
		if(!empty($_POST['LS_DDBH'])) {
			$map['LS_DDBH'] = $_POST['LS_DDBH'];
			$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
			$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
			$OrderDetails = M ( "OrderList" );
			
			$total = $OrderDetails->where($map)->count (); // 计算总数
			import("ORG.Util.Page");// 导入分页类
			$Orders = array ();
			$Orders = $OrderDetails->where($map)->limit ( ($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
			$resultJson = json_encode($Orders);
			if($resultJson == "null"){//如果查询结果返回为null
				$resultJson = "[]";
			}
		}else {
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}
		
	
	/**
	 * 获取学生列表
	 */
	public function studentList() {
		$map =  array ();
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$Student = M ( "Student" );
		
		$xqid =  $_SESSION['XqFilterString'] ;//获取用户当前拥有校区权限
		if(!empty($xqid)){
			$xqid = str_replace("'","",$xqid);
			$map['XS_XQ']  = array('in',$xqid);
		}
		
		if(!empty($_POST['KHMC'])) {
			$KCMC = $_POST['KHMC'];
			$keyword = "%". $KCMC ."%";
			$where['_logic'] = 'or';
			$where['XS_XM'] = array('like',$keyword );
			$where['XS_PY'] = array('like',$keyword );
			$where['XS_PYSX'] = array('like',$keyword );
			$where['XS_XH'] = array('like',$keyword );
			$map['_complex'] = $where;
		}
		
		$map['XS_ZT'] = 'xszt1'; //表明活跃的学生
		
		$total = $Student->where($map)->count (); // 计算总数
		//$list = $db -> field( "$table.art_id,$table.art_title,$table.art_content,$table.art_description,$table2.cate_id,$table2.cate_name" ) ->
		$studentList = array ();
		$studentList = $Student->where($map)->limit ( ($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
		$resultJson = json_encode($studentList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}
		
	/**
	 * 获取优惠劵列表
	 */
	public function couponList() {
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$Coupon = M ( "Coupon" );
		$fix = C( "DB_PREFIX" );				
		import("ORG.Util.Page");// 导入分页类	
			
		$nowDate = date("Y-m-d");	
		$map['status'] = '1';
		$map['YHK_KSSJ']=array('elt',$nowDate);
		$map['YHK_JSSJ']=array('egt',$nowDate);		
		
		if(!empty($_POST['YHK_MC'])) {
			$YHK_MC = $_POST['YHK_MC'];
			$keyword = "%". $YHK_MC ."%";
			$where['_logic'] = 'or';
			$where['YHK_BH'] = array('like',$keyword );
			$where['YHK_MC'] = array('like',$keyword );
			$map['_complex'] = $where;
		}
		
		$total = $Coupon->where($map)->count (); // 计算总数
		$couponList = array ();
		$couponList = $Coupon->where($map)->limit ( ($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
		$resultJson = json_encode($couponList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}	

}
?>
